package org.hackystat.app.projectstatus.view;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.hackystat.app.stdcmd.alert.newdata.NewData;
import org.hackystat.core.common.project.Project;
import org.hackystat.core.common.project.ProjectManager;
import org.hackystat.core.kernel.command.Command;
import org.hackystat.core.kernel.mvc.Page;
import org.hackystat.core.kernel.mvc.RequestHook;
import org.hackystat.core.kernel.user.User;

/**
 * RequestHook for determing the state of the project specific 
 * alerts using the jsp page AlertManager.jsp.
 * 
 * @author Josh
 *
 */
public class AlertManagerSelector implements RequestHook {
  /** Options */
  private static TreeMap selectorMap = null;
  

  /**
   * Run before a page containing AlertManager.jsp is displayed to the user, 
   * and sets the checkboxes associated with each Project.
   *
   * @param user The user associated with this request.
   * @param page The page to be displayed.
   * @param request The request associated with this display.
   */
  public void beforePageDisplayHook(User user, Page page, HttpServletRequest request) {
    // Initialize the selectorMap if first time.
    if (selectorMap == null) {
      selectorMap = new TreeMap();
      selectorMap.put("enabled", "enabled");
      selectorMap.put("disabled", "disabled");
    }
    // Set the options and default value for the LocaleWindowJSP selector.
    request.setAttribute("ProjectStatusOptions", selectorMap);
    request.setAttribute("ProjectStatusDefault", AlertEmailManager.isEnabled(user) ? "enabled" : "disabled");
  }

  /**
   * Runs after a command is received containing AlertManager.jsp.
   *
   * @param user The user associated with this request.
   * @param command The command submitted by this user.
   * @param request The request associated with this command.
   */
  public void afterCommandReceivedHook(User user, Command command, HttpServletRequest request) {
    
    Map<Project, String> enableMap = new HashMap<Project, String>();
    
    ProjectManager manager = ProjectManager.getInstance();
    Set<Project> projectSet = manager.getProjectsWithOwner(user);
    
    int x = 0;
    
    for (Project project : projectSet) {
      if ("enabled".equals(request.getParameter("ProjectStatus"))) {
        enableMap.put(project, "enabled");
      }
      else {
        enableMap.put(project, "disabled");
      }
//      AlertEmailManager.setEnableMap(enableMap);
      AlertEmailManager.setEnabled(user, project, "enabled".equals(request.getParameter("ProjectStatus")));
      x++;
    }
    

    
  }


}
